package com.hand.springboot.controller;


import com.hand.springboot.entity.student;
import com.hand.springboot.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @ Description   :  controller直接调用repisitory 因为比较简单 所以不经过service层
 * @ Author        :  xiong.yilin@hand-china.com
 * @ CreateDate    :  2019/8/8 20:05
 */
@RestController
@RequestMapping("/students")
public class StudentHandler {
    //自动注入
    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/findAll")
    public Collection<student> findAll() {
        return studentRepository.findAll();
    }

    @GetMapping("/findById/{id}")
    public student findById(@PathVariable("id") long id){
        return studentRepository.findById(id);
    }

    @PostMapping("/update")
    public void update(@RequestBody student t){
        studentRepository.saveOrUpdate(t);
    }

    @PostMapping("/save")
    public void save(@RequestBody student t){
        studentRepository.saveOrUpdate(t);
    }

    @DeleteMapping("/delete/{id}")
    public void deleteStudent(@PathVariable("id") long id){
        studentRepository.deleteById(id);
    }
}
